//
//  HighvioAnnotationView.m
//  CarLife
//
//  Created by yu on 2018/7/12.
//  Copyright © 2018年 jpas. All rights reserved.
//

#import "HighvioAnnotationView.h"

#import "HighVioModel.h"

@implementation HighvioAnnotationView

- (id)initWithAnnotation:(id<MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if (self) {
        
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    PPLog(@"select or unselect = %@",selected?@"选":@"非选");
    
    if (self.selected == selected) {
        return;
    }
    
    if (selected) {
    
        CLLocationCoordinate2D coorinate = [self.annotation coordinate];
        for (int i = 0; i < _dataSource.count; i++){
            HighVioModel *vioM = _dataSource[i];
            if ([vioM.lat doubleValue] == coorinate.latitude && [vioM.lng doubleValue] == coorinate.longitude) {
                
                if (self.selectHighvioViewBlock) {
                    self.selectHighvioViewBlock(vioM,YES);
                }
                
                NSInteger num = [[vioM num] integerValue];
                if (num > 0 && num < 50) {
                    self.image = IMAGE_NAMED(@"lHighvio_big");
                } else if (num >= 50 && num < 100) {
                    self.image = IMAGE_NAMED(@"mHighvio_big");
                } else {
                    self.image = IMAGE_NAMED(@"hHighvio_big");
                }
            }
        }
        
    } else {
        CLLocationCoordinate2D coorinate = [self.annotation coordinate];
        for (int i = 0; i < _dataSource.count; i++){
            HighVioModel *vioM = _dataSource[i];
            if ([vioM.lat doubleValue] == coorinate.latitude && [vioM.lng doubleValue] == coorinate.longitude) {
                
                if (self.selectHighvioViewBlock) {
                    self.selectHighvioViewBlock(vioM,NO);
                }
                
                NSInteger num = [[vioM num] integerValue];
                if (num > 0 && num < 50) {
                    self.image = IMAGE_NAMED(@"lHighvio");
                } else if (num >= 50 && num < 100) {
                    self.image = IMAGE_NAMED(@"mHighvio");
                } else {
                    self.image = IMAGE_NAMED(@"hHighvio");
                }
            }
        }
    }
    
    [super setSelected:selected animated:animated];
}

- (void)setDataSource:(NSMutableArray *)dataSource
{
    _dataSource = dataSource;
    
    CLLocationCoordinate2D coorinate = [self.annotation coordinate];
    for (int i = 0; i < _dataSource.count; i++){
        HighVioModel *vioM = _dataSource[i];
        if ([vioM.lat doubleValue] == coorinate.latitude && [vioM.lng doubleValue] == coorinate.longitude){
            
            NSInteger num = [[vioM num] integerValue];
            if (num > 0 && num < 50) {
                self.image = IMAGE_NAMED(@"lHighvio");
            } else if (num >= 50 && num < 100) {
                self.image = IMAGE_NAMED(@"mHighvio");
            } else {
                self.image = IMAGE_NAMED(@"hHighvio");
            }
        }
    }
}


@end
